package lihua.robot.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lihua.robot.common.Result;
import lihua.robot.mapper.FallingTimeMapper;
import lihua.robot.pojo.FallingTime;
import lihua.robot.service.FallingTimeService;
import lihua.robot.utils.ApplicationContextUtil;
import lihua.robot.utils.PostManUtil;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping(value="/robot_fallingTime")
public class FallingTimeController {
    @Autowired
    public FallingTimeService fallingTimeService;

    @Autowired
    public FallingTimeMapper fallingTimeMapper;

    /**
     * 查询已完成，未完成情况
     * @return
     */
    @GetMapping("/querySmallBucket")
    public Result<Map<String,Object>> querySmallBucket(@RequestParam("artificialStatus") int artificialStatus){
        LambdaQueryWrapper<FallingTime> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FallingTime::getArtificial,1)
                .eq(FallingTime::getArtificialStatus,artificialStatus);
        List<FallingTime> list = fallingTimeService.getBaseMapper().selectList(wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        return Result.success(map,"查询成功");
    }

    /**
     * 修改小车运单情况
     * @return
     */
    @PostMapping("/updateAstatus")
    public boolean updateAstatus(@RequestBody FallingTime fallingTime){
        LambdaUpdateWrapper<FallingTime> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(FallingTime::getId,fallingTime.getId())
                .set(FallingTime::getArtificialStatus,fallingTime.getArtificialStatus())
                .set(FallingTime::getSmallBucketStatus,1);
        int count = fallingTimeMapper.update(null,wrapper);

        return count > 0;
    }

    /**
     *
     * @return
     */
    @GetMapping("/querySmallBucket1")
    public Result querySmallBucket1(){
        return Result.success(fallingTimeService.getSmallBucket(),"查询成功");
    }

    /**
     *
     * @return
     */
    @GetMapping("/save")
    public Result saveFallingTime(@RequestParam("orderId") String orderId) {
        PostManUtil.getOrderDetails(orderId);
        return Result.success(PostManUtil.getOrderDetails(orderId),"查询成功");
    }




}
